﻿/* 
 
This file is part of Ingo Karstein's Custom SharePoint 2010 Ribbon project

**Do not remove this comment**

Please see the project homepage at CodePlex:
  http://spcustomribbondemo.codeplex.com

Please see my blog:
  http://ikarstein.wordpress.com

Licence: GPLv2
    Ingo Karstein's SharePoint 2010 Custom Ribbon Demo
    Copyright (C) 2011  Ingo Karstein, ikarstein@hotmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

using System;
using System.Security.Permissions;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Diagnostics;
using System.Xml;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace ik.SharePoint2010.SPCustomTabRibbonDemo.Layouts.SPCustomTabRibbonDemo
{
    public partial class AllItems : WebPartPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        private string tab = @"
          <Tab Id=""ik.SharePoint2010.Ribbon.CustomTab"" Title=""ik-ribbon"" Description=""Ingo Karstein's Command Group"" Sequence=""501"">
            <Scaling Id=""ik.SharePoint2010.Ribbon.CustomTab.Scaling"">
              <MaxSize Id=""ik.SharePoint2010.Ribbon.CustomTab.Group1.MaxSize""
                       GroupId=""ik.SharePoint2010.Ribbon.CustomTab.Group1""
                       Size=""OneLarge""/>
              <Scale Id=""ik.SharePoint2010.Ribbon.CustomTab.Group1.Scaling.CustomTabScaling""
                     GroupId=""ik.SharePoint2010.Ribbon.CustomTab.Group1""
                     Size=""OneLarge""/>
              <MaxSize Id=""ik.SharePoint2010.Ribbon.CustomTab.Group2.MaxSize""
                       GroupId=""ik.SharePoint2010.Ribbon.CustomTab.Group2""
                       Size=""OneLarge""/>
              <Scale Id=""ik.SharePoint2010.Ribbon.CustomTab.Group2.Scaling.CustomTabScaling""
                     GroupId=""ik.SharePoint2010.Ribbon.CustomTab.Group2""
                     Size=""OneLarge""/>
              <MaxSize Id=""ik.SharePoint2010.Ribbon.CustomTab.Group3.MaxSize""
                       GroupId=""ik.SharePoint2010.Ribbon.CustomTab.Group3""
                       Size=""OneLarge""/>
              <Scale Id=""ik.SharePoint2010.Ribbon.CustomTab.Group3.Scaling.CustomTabScaling""
                     GroupId=""ik.SharePoint2010.Ribbon.CustomTab.Group3""
                     Size=""OneLarge""/>
            </Scaling>
            <Groups Id=""ik.SharePoint2010.Ribbon.CustomTab.Groups"">
              <Group Id=""ik.SharePoint2010.Ribbon.CustomTab.Group1""
                     Description=""Ingo Karstein's Custom Ribbon Demo 1""
                     Title=""Ingo Karstein's Custom Ribbon Demo 1""
                     Sequence=""10""
                     Template=""ik.SharePoint2010.Ribbon.Templates.OneLarge"">
                <Controls Id=""ik.SharePoint2010.Ribbon.CustomTab.Group1.Controls"">
                  <Button Id=""ik.SharePoint2010.Ribbon.CustomTab.Group1.Button1""
                          Command=""ik.SharePoint2010.Command.Button1""
                          Description=""Do something""
                          Sequence=""10""
                          LabelText=""Do something""
                          Image16by16=""/_layouts/images/DOC16.GIF""
                          Image32by32=""/_layouts/images/DOC32.GIF""
                          TemplateAlias=""cust1""/>
                </Controls>
              </Group>
              <Group Id=""ik.SharePoint2010.Ribbon.CustomTab.Group3""
                     Description=""Ingo Karstein's Custom Ribbon Demo 3""
                     Title=""Ingo Karstein's Custom Ribbon Demo 3""
                     Sequence=""30""
                     Template=""ik.SharePoint2010.Ribbon.Templates.OneLarge"">
                <Controls Id=""ik.SharePoint2010.Ribbon.CustomTab.Group3.Controls"">
                  <Button Id=""ik.SharePoint2010.Ribbon.CustomTab.Group3.Button3""
                          Command=""ik.SharePoint2010.Command.Button3""
                          Description=""Do more""
                          Sequence=""10""
                          LabelText=""Do more""
                          Image32by32=""/_layouts/images/PPEOPLE.GIF""
                          TemplateAlias=""cust1""/>
                </Controls>
              </Group>
              <Group Id=""ik.SharePoint2010.Ribbon.CustomTab.Group2""
                     Description=""Ingo Karstein's Custom Ribbon Demo 2""
                     Title=""Ingo Karstein's Custom Ribbon Demo 2""
                     Sequence=""20""
                     Template=""ik.SharePoint2010.Ribbon.Templates.OneLarge"">
                <Controls Id=""ik.SharePoint2010.Ribbon.CustomTab.Group2.Controls"">
                  <Button Id=""ik.SharePoint2010.Ribbon.CustomTab.Group2.Button2""
                          Command=""ik.SharePoint2010.Command.Button2""
                          Description=""Do nothing""
                          Sequence=""10""
                          LabelText=""Do nothing""
                          Image32by32=""/_layouts/images/PPEOPLE.GIF""
                          TemplateAlias=""cust1""/>
                </Controls>
              </Group>
            </Groups>
          </Tab>

";
        private string tabTempl = @"
          <GroupTemplate Id=""ik.SharePoint2010.Ribbon.Templates.OneLarge"">
            <Layout Title=""OneLarge"" LayoutTitle=""OneLarge"">
              <Section Alignment=""Top"" Type=""OneRow"">
                <Row>
                  <ControlRef DisplayMode=""Large"" TemplateAlias=""cust1"" />
                </Row>
              </Section>
            </Layout>
          </GroupTemplate>
";

        protected override void OnPreRender(EventArgs e)
        {
            //Debugger.Break();
            SPRibbon r = Microsoft.SharePoint.WebControls.SPRibbon.GetCurrent(this.Page);
            XmlDocument rx = new XmlDocument();
            rx.LoadXml(tab);
            r.RegisterDataExtension(rx.FirstChild, "Ribbon.Tabs._children");
            rx.LoadXml(tabTempl);
            r.RegisterDataExtension(rx.FirstChild, "Ribbon.Templates._children");

            List<IRibbonCommand> commands = new List<IRibbonCommand>();
            commands.Add(new SPRibbonCommand("ik.SharePoint2010.Command.Button1", "ikSharePoint2010CommandButton1Action()", "true"));
            commands.Add(new SPRibbonCommand("ik.SharePoint2010.Command.Button2", "ikSharePoint2010CommandButton2Action()", "ikSharePoint2010CommandButton2Enable()"));
            commands.Add(new SPRibbonCommand("ik.SharePoint2010.Command.Button3", "ikSharePoint2010CommandButton3Action()", "ikSharePoint2010CommandButton3Enable()"));

            SPRibbonScriptManager rsm = new SPRibbonScriptManager();

            ScriptLink.RegisterScriptAfterUI(this.Page, "SP.Runtime.js", false, true);
            ScriptLink.RegisterScriptAfterUI(this.Page, "SP.js", false, true);
            ScriptLink.RegisterScriptAfterUI(this.Page, "CUI.js", false, true);
            ScriptLink.RegisterScriptAfterUI(this.Page, "SP.Ribbon.js", false, true);
            ScriptLink.RegisterScriptAfterUI(this.Page, "ik.SharePoint2010.SPCustomTabRibbonDemo/ikactions.js", false, true);
            ScriptLink.RegisterScriptAfterUI(this.Page, "ik.SharePoint2010.SPCustomTabRibbonDemo/ikribbon.UI.js", false, true);

            rsm.RegisterGetCommandsFunction(this.Page, "getGlobalCommands", commands);
            rsm.RegisterCommandEnabledFunction(this.Page, "commandEnabled", commands);
            rsm.RegisterHandleCommandFunction(this.Page, "handleCommand", commands);

            string script = @"
                <script language=""javascript"" defer=""true"">
                //<![CDATA[
                   function ikribbonInit1() { 
                     ikribbonInit();
                   }
                   ExecuteOrDelayUntilScriptLoaded(ikribbonInit1, 'ikribbon.UI.js');
                //]]>
                </script>";
            ClientScript.RegisterClientScriptBlock(this.GetType(), "InitPageComponent", script);

            r.MakeTabAvailable("ik.SharePoint2010.Ribbon.CustomTab");
            r.SetInitialTabId("ik.SharePoint2010.Ribbon.CustomTab", "");
            base.OnPreRender(e);
        }

        protected override void OnInitComplete(EventArgs e)
        {
            base.OnInitComplete(e);
        }
    }
}
